﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.IO;
using Blueprints.Resolution;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Blueprints
{
    internal sealed class SerializationScope : IDisposable
    {
        private readonly Dictionary<object, StartNode> startNodes = new Dictionary<object, StartNode>();
        private readonly BlueprintContext context;

        public SerializationScope(BlueprintContext context, Uri targetUri)
        {
            this.context = context;
            TargetUri = targetUri;
            Nodes = new List<INode>();
        }

        public Uri TargetUri
        {
            get;
            private set;
        }

        public bool TryGetSerializedNode(object instance, out Uri link)
        {
            if (context.TryGetRegisteredUri(instance, out link))
            {
                return true;
            }

            StartNode startNode;
            if (!startNodes.TryGetValue(instance, out startNode))
            {
                link = null;
                return false;
            }

            if (string.IsNullOrEmpty(startNode.Id))
            {
                startNode.Id = Guid.NewGuid().ToString();
            }

            link = new Uri(TargetUri, "#" + startNode.Id);
            return true;
        }

        public void SetSerializedNode(object instance, StartNode startNode)
        {
            if (context.RequiresId(instance))
            {
                if (string.IsNullOrEmpty(startNode.Id))
                {
                    startNode.Id = Guid.NewGuid().ToString();
                }

                Uri uri = new Uri(TargetUri, "#" + startNode.Id);
                context.Register(instance, uri);
                return;
            }

            startNodes[instance] = startNode;
        }

        public void SetSerializedValue(object instance, StartNode startNode, Type type, string content)
        {
            if (context.RequiresId(instance))
            {
                if (string.IsNullOrEmpty(startNode.Id))
                {
                    startNode.Id = Guid.NewGuid().ToString();
                }

                Uri uri = new Uri(TargetUri, "#" + startNode.Id);
                context.Register(instance, uri);
                return;
            }

            if (type.GetTypeInfo().IsValueType || type == typeof(string))
            {
                if (content.Length <= 64)
                {
                    return;
                }
            }

            startNodes[instance] = startNode;
        }

        public List<INode> Nodes { get; private set; }

        void IDisposable.Dispose()
        {
            startNodes.Clear();
        }
    }
}